iT邦幫忙

2022 iThome 鐵人賽

DAY 24
0

As a chess enthusiast, you would like to write your own version of the game.Yes, there maybe plenty of implementations of chess available online already, but yours will be unique!

But before you can let your imagination run wild, you need to take care of the basics. Let's start by generating the board.

Each square of the chessboard is identified by a letter-number pair. The vertical columns of squares, called files, are labeled A through H. The horizontal rows of squares, called ranks, are numbered 1 to 8.

1. Define the rank range

Implement the rank_range/0 function. It should return a range of integers, from 1 to 8.

2. Define the file range

Implement the file_range/0 function. It should return a range of integers, from the code point of the uppercase letter A, to the code point of the uppercase letter H.

3. Transform the rank range into a list of ranks

Implement the ranks/0 function. It should return a list of integers, from 1 to 8. Do not write the list by hand, generate it from the range returned by the rank_range/0 function.

4. Transform the file range into a list of files

Implement the files/0 function. It should return a list of letters (strings), from "A" to "H". Do not write the list by hand, generate it from the range returned by the file_range/0 function.


https://exercism.org/tracks/elixir/exercises/chessboard

defmodule Chessboard do
  @moduledoc """
  practice range
  """

  def rank_range, do: 1..8

  def file_range, do: ?A..?H

  def ranks, do: rank_range() |> Enum.to_list

  def files, do: file_range() |> Enum.map(&(<<&1>>))
end

上一篇
二十三. File
下一篇
二十五. Structs
系列文
戀戀 Elixir30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言